package com.crm.web.controller;

import com.crm.entity.Customer;
import com.crm.entity.Dict;
import com.crm.service.CustomerService;
import com.crm.service.DictService;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.annotation.Resource;
import java.util.List;

@Controller
@RequestMapping("/customer")
public class CustomerController {
    @Resource
    CustomerService custmoerService;

    @Resource
    DictService dictService;

    @RequestMapping(value = "/list")
    public String list(Model model, String custName, String custRegion,
                       @RequestParam(required = false, defaultValue = "1") int pageIndex) {
        System.out.println("名称为:" + custName);
        Sort sort = Sort.by(Sort.Direction.ASC, "custNo");
        //控制分页的辅助类，可以设置页码(从0开始！！！)、每页的数据条数、排序等
        Pageable pageable = PageRequest.of(pageIndex - 1, 3, sort);
        Page<Customer> customerPager = custmoerService.findCustomer(custName, custRegion, pageable);
        model.addAttribute("page", customerPager);
        model.addAttribute("custName", custName);
        model.addAttribute("custRegion", custRegion);
        List<Dict> dictList = dictService.findSvrType1("地区");
        model.addAttribute("dictList", dictList);
        return "customer/list";
    }

    @RequestMapping(value = "/edit")
    public String edit(String customercId, Model model) {
        System.out.println(customercId);
        return "customer/edit";
    }
}
